<template>
    <div class="tableContainer">
        <el-form :inline="true"  class="demo-form-inline" @submit.native.prevent>
            <el-form-item label="">
                <el-input class="elInput" v-model="key" placeholder="输入单位名称"></el-input>
            </el-form-item>
            
            <el-form-item>
                <el-button class="elBtn" type="primary" @click="selectUnit">查询</el-button>
                <el-button class="elBtn" type="primary" @click="openDialog('','',1)">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" max-height="88%" row-key="id" border default-expand-all :tree-props="{children: 'childList', hasChildren: 'hasChildren'}" style="width: 100%" >
            <el-table-column prop="name" label="单位名称" min-width="20%" align="center"> </el-table-column>
            <el-table-column prop="createTime" label="创建时间" min-width="20%" align="center" :formatter="this.$common.dateFormat"></el-table-column>
            <el-table-column prop="status" label="状态" align="center" min-width="15%" :formatter="this.$common.statusFormat2"> </el-table-column>
            <el-table-column prop="op" label="操作" align="center" min-width="20%">
                <template slot-scope="scope">
                    <el-button class="elBtn"  type="primary" size="mini" icon="edit"  @click="openDialog(scope.$index, scope.row,2)">修改</el-button>
                    <el-button size="mini" icon="delete" type="danger" @click="openDialog(scope.$index, scope.row,3)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- <el-pagination background 
			layout="prev, pager, next, sizes, total, jumper"
			:page-sizes="pagesizes"
			:page-size="pagesize"
			:total="tableData.length"
            @current-change="changePage"
            @size-change="sizeChange"
			>
		</el-pagination> -->

        <!-- 对话框 -->
        <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="30%" center @close="clearForm">
            <el-form ref="form" :model="form">
                <el-form-item label="单位名称" :label-width="formLabelWidth" prop="name" :rules="{validator:this.$common.emptyValidate, trigger: 'blur'}">
                    <el-input class="elInput" v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="上级单位" :label-width="formLabelWidth" prop="supId">
                    <select-tree v-model="form.supId"  placeholder="请选择" :options="options" />
                </el-form-item>
                
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <!-- <el-input class="elInput" v-model="form.status" autocomplete="off"></el-input> -->
                    <el-switch v-model="status" active-color="#13ce66" inactive-color="#ccc"  inactive-text="禁用" active-text="启用">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="clearForm">取 消</el-button>
                <el-button class="elBtn" type="primary" @click="saveUnit">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="提示" :visible.sync="centerDialogVisible" width="30%" center>
            <span>确定删除这条单位信息</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible = false">取 消</el-button>
                <el-button class="elBtn" type="primary" @click="doDelete">确 定</el-button>
            </span>
        </el-dialog>
    </div>
    

</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
    data() {
        return {
            dialogFormVisible:false, // 控制新增/修改对话框的显示隐藏
            centerDialogVisible:false, // 控制删除对话框的显示隐藏
            unitId:"", // 选择的角色id
            dialogTitle:"新增单位", // 对话框标题
            pagesizes:[10,15,20,25],
            pagesize:10,
            currpage: 1,
            status:true,
            key: '', // 搜索内容
            form: { // 新增/修改表单参数
                id:"",
                name: '',
                supId:"",
                status:true
            },
            options: [],
            tableData: [],
            formLabelWidth: '20%', // label宽度
            tipMsg:"",
            clientHeight:"" // 屏幕高度
      }
    },
    created(){
        // 获取屏幕高度
        this.clientHeight = this.$common.getClientHeight()
        this.pagesize = Math.ceil((this.clientHeight - 260)/55)
        this.pagesizes[0] = this.pagesize

        this.selectUnit()
        this.selectAllUnit()
    },
    methods:{
        // 打开对话框
        openDialog(index,row,type){
            if(type == 3){ // 删除
                this.centerDialogVisible = true
                this.unitId = row.id
            }else if(type == 2){ // 修改
                
                this.unitId = row.id
                this.selectById()
                this.dialogTitle = "修改单位信息"
            }
            else if(type == 1){ // 新增
                this.dialogFormVisible = true
                this.dialogTitle = "新增单位信息"
            }
        },
        // 查询所有单位
        selectAllUnit(){
            this.$common.selectAllUnit(res => {
                this.options = this.$common.dataFormat(res.data.data)
                this.options.unshift({label:"请选择",id:""})
            })
            
        },
        // 查询单位
        selectUnit(){
            const params = {}
            params.key = this.key
            this.$api.selectUnit(params)
            .then(result=>{
                var _this = this
                this.$common.filterLogin(result,_this,()=>{
                   if(result.data.success){
                        _this.tableData = result.data.data
                    }
                })
            })
        },
        // 根据id查询单位
        selectById(){
            this.$api.selectUnitById({params:{id:this.unitId}})
            .then(result=>{
                var _this = this
                this.$common.filterLogin(result,_this,()=>{
                    if(result.data.success){
                        result.data.data.supId = result.data.data.supId == "0"?null:result.data.data.supId
                        _this.form = result.data.data
                        _this.status = (result.data.data.status == 1)?true:false
                        _this.dialogFormVisible = true
                    }
                })
            
                
            })
        },
        // 修改/新增时的保存
        saveUnit(){
            this.$refs['form'].validate((valid) => {
                if(valid){
                    this.form.status = this.status?1:0
                    if(!this.form.id){ // 新增
                        this.$api.addUnit(this.form)
                        .then(result=>{
                            this.successFun(result,1)
                        })
                    }else{ // 修改
                        this.$api.modifyUnit(this.form)
                        .then(result=>{
                            this.successFun(result,2)
                        })
                    }
                }
            })
        },
        // 删除用户信息
        doDelete(){
            this.$api.deleteUnit({id:this.unitId}).then(result=>{
                this.successFun(result,3)
            })
        },
        // 接口调用成功的方法
        successFun(result,type){
            var _this = this
            this.$common.filterLogin(result,_this,()=>{
                _this.tipMsg = result.data.message
                // 重新查询
                _this.selectUnit()
                var _this2 = _this
                // 清空表单
                _this.clearForm()
                _this.form.id = ""
                if(result.data.success){
                    this.$message({center: true,message:_this.tipMsg?_this.tipMsg:"操作成功",type: 'success'});
                }else{
                    this.$message({center: true,message: result.data.message?result.data.message:"网络错误,请稍后再试",type: 'warning'});
                }
                
            })
            
        },
        // 清空表单
        clearForm(){
            this.dialogFormVisible = false
            this.centerDialogVisible = false
            var _this = this
            // this.$nextTick(() => {
            //     if (_this.$refs.form !== undefined) {
            //         _this.$refs.form.resetFields()
            //     }
            // })
            // 清楚表单数据
            for(let item in this.form){
                this.form[item] = ""
            }
            // this.form.id = ""
            this.$refs['form'].clearValidate()
        },
        // 分页--改变当前页
        changePage(index){
            this.currpage = index
            this.selectUnit()
        },
        // 分页--改变当前页显示条数
        sizeChange(page){
            this.pagesize = page
            this.selectUnit()
            
        }
    },
    components: {
      'select-tree': Treeselect
    }
}
</script>

<style lang="scss" scoped>
.tableContainer{
    height: 97%;
    padding: 10px;
    background: #FDFDFD;
    // background: transparent;
    border-radius: 5px;
    // box-shadow: 5px 5px 4px rgba(204, 204, 204, 0.5)
    .el-pagination.is-background{
        text-align: right;
        padding:10px 5px;
    }
    .el-table,.el-table th, .el-table tr{
        background: transparent !important;
    }
}
</style>